package com.jiazhong.servlets;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @Auther:shuXin
 */
/*
创建Servlet的这个实体类，这个类要实现servlet的接口(但是实现的方法比较多，所以我们继承一盒HttpServlet类)
 */
//@WebServlet(name = "ServletTest01",
//        urlPatterns = {"/ServletTest01", "/ServletTest02"},
//        initParams = {@WebInitParam(name = "encoding", value = "UTF-8")},
//        loadOnStartup = 6)
@WebServlet("/servletTest01")
public class ServletTest01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受前端的数据，使用request对象来进行接收
        //获得数据之前给数据进行设置编码集 (不推荐)
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        System.out.println(username);
        //判断用户名是否已经存在
        if ("zhangsan".equals(username)) {
            //使用打印流打印到页面中
            response.getWriter().write("yonghumingyijingcunzai");

        } else {
            response.getWriter().write("yonghumingkeyong");
        }
    }
}
